#include "ProcessManager.h"

void ProcessManager::Attach(Process *newProcess)
{
	ProcessList.push_back(newProcess);
}
bool ProcessManager::IsEmpty()
{
	return ProcessList.empty();
}
bool ProcessManager::IsProcessActive(char name[])
{
	std::list<Process*>::iterator Itor = ProcessList.begin();

	//Traverse the list looking for name[]
	while (Itor != ProcessList.end())
	{
		if(!strcmp((*Itor)->GetType(),name))
		{
			return true;
		}
	}
	return false;
}
void ProcessManager::UpdateProcesses(int milliseconds)
{
	if (IsEmpty())
	{
		return;
	}
	std::list<Process*>::iterator Itor = ProcessList.begin();

	//Traverse the list running each process
	while (Itor != ProcessList.end())
	{
		if ((*Itor)->GetKill())
		{
			//Kill the object, but first check if there is a child to add
			if (!(*Itor)->GetNext())
			{
				//No child, delete the object
				ProcessList.remove((*Itor));
				break;
			}
			else
			{
				//Attach child and delete parent
				(*Itor)->GetNext()->InitializeProcess();
				Attach((*Itor)->GetNext());
				ProcessList.remove((*Itor));
				break;
			}
		}
		else
		{
			//Object isnt dead, run it
			(*Itor)->UpdateProcess(milliseconds);
		}
		Itor++;
	}
}